package com.sm.controller;


import cn.dev33.satoken.annotation.SaCheckRole;
import com.github.pagehelper.PageInfo;
import com.sm.controller.request.SelectCourseRequest;
import com.sm.controller.request.SelectCourseStudentRequest;
import com.sm.controller.request.TeacherRegisterRequest;
import com.sm.controller.request.UpdateStudentScoreRequest;
import com.sm.controller.respond.SelectCourseAchievementRespond;
import com.sm.controller.respond.SelectCourseStudentRespond;
import com.sm.entity.Course;
import com.sm.entity.Student;
import com.sm.entity.Teacher;
import com.sm.service.AdminService;
import com.sm.service.TeacherService;
import com.sm.service.UserService;
import com.sm.utils.R;
import com.sm.utils.RD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @Description: Controller
 * @Author: lpf
 * @Date: 2022/2/16
 */
@RestController

public class TeacherController {

    @Autowired(required = false)
    TeacherService teacherService;

    @Autowired(required = false)
    private AdminService adminService;

    @Autowired(required = false)
    UserService userService;

    /**
     * @Description: 老师查看课程列表
     * @Author: lpf
     * @Date: 2022/2/16
     */
    @SaCheckRole("teacher")
    @GetMapping("/teacher/course/list")
    RD getCourseList(@RequestParam("page") Integer page, @RequestParam("limit") Integer size, @RequestParam("sort") Integer sort) {
        PageInfo<Course> list = teacherService.getAllCourse(page, size);
        return RD.success().putData("total", list.getTotal()).putData("items", list.getList());
    }


    /**
     * @Description: 查询某个课程学生成绩
     * @Author: lpf
     * @Date: 2022/2/16
     */
    @SaCheckRole("teacher")
    @PostMapping("/teacher/courseStudentAchievement")
    RD getCourseStudentAchievement(@RequestBody SelectCourseStudentRequest request) throws Exception {
        List<SelectCourseAchievementRespond> list = teacherService.getStudentAchievement(request);
        if (request.getStudentName() != "") {
            for (int i = 0; i < list.size(); i++) {
                if (!list.get(i).getName().equals(request.getStudentName())) {
                    list.remove(i--);
                }
            }
        }
        return RD.success().putData("items", list).putData("total", list.size());
    }

    @SaCheckRole("teacher")
    @GetMapping("/teacher/courseStudentScore")
    RD getCourseStudentScore(@RequestParam("courseId") Integer courseId) {
        List<SelectCourseAchievementRespond> list = teacherService.getStudentAchievement(courseId);
        return RD.success().putData("items", list).putData("total", list.size());
    }


    /**
     * @Description: 老师查看某个课程的选课学生
     * @Author: lpf
     * @Date: 2022/2/16
     */
    @SaCheckRole("teacher")
    @PostMapping("/teacher/courseStudent")
    RD getCourseStudent(@RequestBody SelectCourseRequest request) throws Exception {
        List<SelectCourseStudentRespond> list = teacherService.getCourseAttender(request);
        if (request.getStudentName() != "") {
            for (int i = 0; i < list.size(); i++) {
                if (!list.get(i).getName().equals(request.getStudentName())) {
                    list.remove(i--);
                }
            }
        }
        return RD.success().putData("items", list).putData("total", list.size());
    }


    /**
     * @Description: 教师获得所有学生信息
     * @Author: lpf
     * @Date: 2022/2/16
     */
    @SaCheckRole("teacher")
    @GetMapping("/teacher/studentList")
    RD getAllStudent() {
        List<Student> list = teacherService.getAllStudent();
        return RD.success().putData("list", list);
    }

    /**
     * @Description: 修改课程信息
     * @Author: lpf
     * @Date: 2022/3/5
     */
    @SaCheckRole("teacher")
    @PostMapping("/teacher/course/update")
    public R modifyCourseInformation(@RequestBody Course course) {
        teacherService.updateCourseList(course);
        return R.success();
    }

    /**
     * @Description: 老师自我注册
     * @Author: lpf
     * @Date: 2022/3/5
     */

    @PostMapping("/teacherRegister")
    public R addTeacher(@RequestBody TeacherRegisterRequest request) throws Exception {
        Teacher teacher = new Teacher(null, request.getUsername(), request.getRealName(), request.getPhone(), request.getAddress());
        adminService.addTeacher(teacher);
        userService.addTeacherUser(request);
        return R.success();
    }

    /**
     * @Description: 老师把某个学生退课
     * @Author: lpf
     * @Date: 2022/3/5
     */
    @SaCheckRole("teacher")
    @GetMapping("/teacher/deleteStudentFromCourse")
    R deleteCourseStudent(@RequestParam("courseId") Integer courseId, @RequestParam("studentName") String studentName) {
        teacherService.deleteCourseStudent(courseId, teacherService.selectStudentByName(studentName));
        return R.success();
    }

    /**
     * @Description: 老师修改自身信息
     * @Author: lpf
     * @Date: 2022/3/5
     */
    @SaCheckRole("teacher")
    @PostMapping("/teacher/update")
    R updateSelf(@RequestBody Teacher teacher) throws Exception {
        teacherService.updateTeacher(teacher);
        userService.updateTeacherUser(teacher);
        return R.success();
    }

    /**
     * @Description: 修改学生成绩
     * @Author: lpf
     * @Date: 2022/3/6
     */
    @SaCheckRole("teacher")
    @PostMapping("/teacher/updateStudentScore")
    R updateScore(@RequestBody UpdateStudentScoreRequest request) {
        Integer studentId = teacherService.selectStudentByName(request.getName());
        teacherService.updateStudentScore(request.getScore(), request.getCourseId(), studentId);
        return R.success();
    }

    /**
     * @Description: 教师注销
     * @Author: lpf
     * @Date: 2022/3/8
     */
    @SaCheckRole("teacher")
    @GetMapping("/teacher/delete")
    R deleteTeacherBySelf() throws Exception {
        userService.deleteTeacher();
        return R.success();
    }

    @GetMapping("/testTeacher")
    String test() {
        return "hello teacher";
    }
}
